/********************************************************************************
Title: Data Preparation for Sierra Leone Sample
		Define time points
		Define variables to use in each index
		Define survey round to use as reference round
********************************************************************************/


use  "${raw}/sierraleone/sierraleone_desc.dta", clear
sort hhid round
collapse (firstnm) age housing1 housing2 housing3 housing4 asset* female, by(hhid)
tempfile desc
save `desc'

use  "${raw}/sierraleone/sierraleone.dta", clear

merge m:1 hhid  using `desc', nogen

loc ref month == 1

*-------------------------------------------------------------------------------
*			0. Formating and Renaming Variables
*-------------------------------------------------------------------------------

*			0.0 Heterogeneity variables
*-------------------------------------------------------------------------------
gl sesvars
foreach var of varlist housing? asset* {
	tab `var', gen(`var'_)
	if !regexm("`var'","asset") gl sesvars $sesvars `var'_?
	if regexm("`var'","asset") gl sesvars $sesvars `var'
}
pca $sesvars, factor(1)
predict ses



*			0.1		PID and HHID
*-------------------------------------------------------------------------------
ren hhid pid
tostring pid, gen(hhid)


*			0.2		Month
*-------------------------------------------------------------------------------
gen d = dofc(date)
drop date 
ren d date
gen month = 1 + (date > mdy(3,1,2020)) if !mi(date)


*-------------------------------------------------------------------------------
*			1. Generating Variable Lists
*-------------------------------------------------------------------------------
macro drop vars
gl vars worry_health1 worry_food worry_health2 worry_job difficulties happiness1 happiness2


replace happiness1 = -1*happiness1
replace worry_health1 = -1*worry_health1
replace worry_food = -1*worry_food
replace worry_health2 = -1*worry_health2 
replace worry_job = -1*worry_job
corr $vars if round == 1

*-------------------------------------------------------------------------------
*			2. Generating Indices
*-------------------------------------------------------------------------------
foreach var in  $vars {
	sum `var' if `ref'
	replace `var' = (`var' - `r(mean)')/`r(sd)'
	gen swt_`var' = `var'
}

factor $vars  if  `ref', ipf factor(1)
predict ___depression_fw

*-------------------------------------------------------------------------------
*          Unweighted indices
*-------------------------------------------------------------------------------
egen ___depression_nw = rowmean($vars)


*-------------------------------------------------------------------------------
*          Inverse-covariance-weighted indices
*-------------------------------------------------------------------------------
icw_index($vars), gen(___depression_icw)

reg ___depression_nw ses

